package com.sunshine.demo;

import java.util.Date;
import java.util.HashMap;
import com.sunshine.demo.util.HttpUtil;
import com.sunshine.demo.po.LoanApply;
import com.sunshine.demo.po.RequsetInfo;
import com.sunshine.demo.util.GsonUtil;
import com.sunshine.demo.util.SignUtil;

public class sendDemo {
	public static String serviceURL = "http://api2.shansudai.cn/execute";
	public static String channelId = "21";		//各个平台所分配的channelId
	public static String md5key = "1234";				//平台的md5 key
	public static String  ver = "2.0";
	public static String productId = "a66ea52c-8f31-4c05-95ae-1ee4c4082c7c";				//分配的productId
	
	/**
	 * 注册
	 */
	public static void register(){
		try{

			//注册demo
			HashMap<String, Object> requestMap = new HashMap<>();
			requestMap.put("idNo", "43060219940528171X");
			requestMap.put("userName", "小李");
			requestMap.put("registerTime", new Date());
			requestMap.put("contactTel", "1311111111");
//			requestMap.put("")
			//发送http/https请求并得到应答
			String returnResponse = HttpUtil.httpPostToJSON(serviceURL, getRequestJson("register", channelId, requestMap));
			//TODO：解析返回的应答，进行业务处理
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/****
	 *  以loanApply为例，演示如何给雁阵科技发送请求
	 */
	public static void loanApply(){
		try{
			
			//对于各个命令的params，如果请求字段比较多，可以定义一个po对象类来进行封装
			LoanApply loanApply = new LoanApply();
			loanApply.setIdNo("13060219940528171X");
			loanApply.setAmount(new Double(10000));
			loanApply.setLoanTerm(90);
			loanApply.setPaymentOption(4);
			loanApply.setProductId(productId);
			//loanApply.setLoanPurpose(new String("测试".getBytes("ISO8859-1"),"UTF-8"));
			loanApply.setLoanPurpose("测试");
			//发送http/https请求并得到应答
			String returnResponse = HttpUtil.httpPostToJSON(serviceURL, getRequestJson("loanApply", channelId, loanApply));
			
			//TODO：解析返回的应答，进行业务处理
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	/****
	 *  以loanContractConfirm为例，演示如何给雁阵科技发送请求
	 */
	public static void loanContractConfirm(){
		try{
			//对于各个命令的params，如果请求字段太少，可以直接使用HashMap进行封装
			HashMap<String, Object> loanContractConfirmParams = new HashMap<String,Object>();
			loanContractConfirmParams.put("loanId", "aecaed7d-cf96-4d41-a12b-51e2305cb8c8");
			loanContractConfirmParams.put("confirmation", 2);
			
			//发送http/https请求并得到应答
			String returnResponse = HttpUtil.httpPostToJSON(serviceURL, getRequestJson("loanContractConfirm", channelId, loanContractConfirmParams));
			
			//TODO：解析返回的应答，进行业务处理
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	/****
	 * 封装发送给雁阵科技的请求的Json串
	 * @param requestParamsObj： 方法消息体对象
	 * @param method： 方法名
	 * @param channelId：渠道编号
	 * @return   请求的json字符串
	 */
	private static String getRequestJson(String method, String channelId, Object requestParamsObj) {
		RequsetInfo<Object> requsetInfo = new RequsetInfo<>();

		requsetInfo.setSignType("MD5");  //使用MD5签名
		String sign = SignUtil.generateMD5Sign(method, ver, channelId, md5key, requestParamsObj);
		requsetInfo.setSign(sign);
		requsetInfo.setVer(ver);
		requsetInfo.setChannelId(channelId);
		requsetInfo.setMethod(method);
		requsetInfo.setParams(requestParamsObj);
		return GsonUtil.getGson().toJson(requsetInfo);
	}
	
	/*****
	 *
	 * @param args
	 */
	public static void main(String[] args) {
		register();
	//	loanApply();
	//	loanContractConfirm();
	}
	
}